package com.sikaryofficial.backend.service.email.notice;

import cn.hutool.core.text.CharSequenceUtil;
import com.sikaryofficial.backend.config.GuildTaskNoticeConfig;
import com.sikaryofficial.backend.constant.EmailConstant;
import com.sikaryofficial.backend.constant.EmailSendSourceEnum;
import com.sikaryofficial.backend.service.email.EmailService;
import com.sikaryofficial.backend.service.user.UserCacheService;
import com.sikaryofficial.system.api.domain.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.MessageFormat;
import java.util.Map;
import java.util.Objects;

/**
 * @author : qinjinyuan
 * @desc : 邮件通知
 * @date : 2023/10/31 19:02
 */
@Service
@Slf4j
public class GuildTaskNoticeService {
    @Resource
    private Map<String, EmailService> emailServiceMap;
    @Resource
    private GuildTaskNoticeConfig noticeConfig;
    @Autowired
    private UserCacheService userCacheService;

    /**
     * 邮件标题: You’ve Earned 10 Points – Spin Again!
     * 内容：
     * Hi ,
     * <p>
     * Congrats! You've earned 10 points for completing your task. Log in now and use your points to spin for a chance at great prizes!【抽奖页网址】
     * <p>
     * Good luck,
     * The HiSMK Team
     *
     * @param points 积分
     * @param userId 用户ID
     */

    @Async("asyncTaskExecutor")
    public void guideEmailNotice(Long userId, Integer points) {
        // 发送邮件及缓存邮箱对象
        String subject = MessageFormat.format(noticeConfig.getGuideSubject(), points);
        String content = MessageFormat.format(noticeConfig.getGuideContent(), points, noticeConfig.getLotteryDrawUrl());
        SysUser sysUser = userCacheService.getSysUser(userId);
        if (Objects.isNull(sysUser) || Objects.isNull(sysUser.getEmail())) {
            log.error("winEmailNotice error: 获取邮件失败");
            return;
        }
        String to = sysUser.getEmail();
        EmailService emailService = emailServiceMap.get(EmailSendSourceEnum.GMAIL.getSendSource() + EmailConstant.SUFFIX);
        emailService.sendHtmlMail(to, subject, content);
    }

    /**
     * 实物中奖用户通知
     * <p>
     * 主题
     * Congratulations! You’ve Won a Prize – Action Needed Within 7 Days
     * <p>
     * 正文
     * Hi [Name],
     * <p>
     * Congratulations!🎉 You’ve won a [Prize Name] in our raffle! To claim your prize, please provide the following information:
     * <p>
     * Full shipping details:
     * 1. Full name
     * 2. Phone number
     * 3. Address
     * 4. Postal code
     * <p>
     * Proof of age (21+):
     * A clear copy of a valid ID (in compliance with regional regulations).
     * <p>
     * Please reply to this email with the requested details within 7 days. Rest assured, your personal information will be handled with strict confidentiality and used solely for prize delivery.
     * <p>
     * If we do not receive the required information within this timeframe, your prize will be forfeited.
     * <p>
     * We look forward to hearing from you soon and sending your prize your way!
     * <p>
     * Best regards,
     * The HiSMK Team
     * [hismk主站网址]
     *
     * @param prizeName 奖品名称
     * @param userId    用户ID
     */
    @Async("asyncTaskExecutor")
    public void winEmailNotice(Long userId, String prizeName, Integer hasCash) {
        SysUser sysUser = userCacheService.getSysUser(userId);
        if (Objects.isNull(sysUser) || Objects.isNull(sysUser.getEmail())) {
            log.error("winEmailNotice error: 获取邮件失败");
            return;
        }
        String to = sysUser.getEmail();
        // 发送邮件及缓存邮箱对象
        String subject = noticeConfig.getWinSubject();
        String content;
        if (Objects.equals(hasCash, 0)) {
            content = MessageFormat.format(noticeConfig.getWinContent(), prizeName, noticeConfig.getHismkHome());
        } else {
            content = MessageFormat.format(noticeConfig.getUserAddressContent(), prizeName, noticeConfig.getHismkHome());
        }
        EmailService emailService = emailServiceMap.get(EmailSendSourceEnum.GMAIL.getSendSource() + EmailConstant.SUFFIX);
        emailService.sendHtmlMail(to, subject, content);
    }

    @Async("asyncTaskExecutor")
    public void physicalGiftsNotice(String email, String prizeName) {
        if (CharSequenceUtil.isBlank(email)) {
            log.error("winEmailNotice1 error: 获取邮件失败");
            return;
        }
        // 发送邮件及缓存邮箱对象
        String subject = noticeConfig.getWinSubject();
        String content = MessageFormat.format(noticeConfig.getWinContent(), prizeName, noticeConfig.getHismkHome());
        EmailService emailService = emailServiceMap.get(EmailSendSourceEnum.GMAIL.getSendSource() + EmailConstant.SUFFIX);
        emailService.sendHtmlMail(email, subject, content);
    }

    /**
     *
     * @param email
     * @param prizeName
     */
    @Async("asyncTaskExecutor")
    public void userAccountNotice(String email, String prizeName) {
        if (CharSequenceUtil.isBlank(email)) {
            log.error("userAccountNotice error: 获取邮件失败");
            return;
        }
        // 发送邮件及缓存邮箱对象
        String subject = noticeConfig.getWinSubject();
        String content = MessageFormat.format(noticeConfig.getUserAddressContent(), prizeName, noticeConfig.getHismkHome());
        EmailService emailService = emailServiceMap.get(EmailSendSourceEnum.GMAIL.getSendSource() + EmailConstant.SUFFIX);
        emailService.sendHtmlMail(email, subject, content);
    }
}
